package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpLogService;
import com.itheima.service.EmpService;
import com.itheima.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpLogService empLogService;

    /*@Override
    public PageResult page(String name, Integer gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {
        //总数据
        Long total = empMapper.count();
        Integer start = (page - 1) * pageSize;
        List<Emp> empList = empMapper.list(start, pageSize);
        return new PageResult(total, empList);
    }*/

    /**
     * 分页查询
     *
     * @param empQueryParam
     * @return
     */
    @Override
    public PageResult page(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
        List<Emp> empList = empMapper.list(empQueryParam);
        Page<Emp> empPage = (Page<Emp>) empList;
        return new PageResult(empPage.getTotal(), empPage.getResult());
    }


    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void saveEmp(Emp emp) {
        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.insert(emp);
            List<EmpExpr> exprList = emp.getExprList();
            if (exprList != null) {
                Integer empId = emp.getId();
                exprList.forEach(empExpr -> empExpr.setEmpId(empId));
                empExprMapper.saveEmpExpr(exprList);
            }
        } finally {
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), "添加用户：" + emp);
            empLogService.insertLog(empLog);
        }

    }

    @Override
    public List<Emp> findAll() {
        return empMapper.findAll();
    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.deleteByIds(ids);
        empExprMapper.deleteByIds(ids);
    }

    @Override
    public Emp getInfo(Integer id) {
        return empMapper.getInfo(id);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void updateById(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        empExprMapper.deleteByIds(Arrays.asList(emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null) {
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            empExprMapper.saveEmpExpr(exprList);
        }
    }

    @Override
    public LoginInfo login(Emp emp) {
        LoginInfo loginInfo = empMapper.loginByUserNameAndPassword(emp);
        if (loginInfo != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginInfo.getId());
            claims.put("username", loginInfo.getUsername());
            LoginInfo info = new LoginInfo(loginInfo.getId(), loginInfo.getUsername(), loginInfo.getName(), JwtUtils.createJwt(claims));
            return info;
        }
        return null;
    }

}
